﻿using Model;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;

namespace DataAccess
{
    public class BreakAwayContext : DbContext
    {   //构造函数中 base 是调用父类构造函数 并传入连接字符串
        public BreakAwayContext() : base("name=BreakAwayContext")
        { }
        public DbSet<Model.Lodging> Lodgings { get; set; }
        public DbSet<Model.Destination> Destinations { get; set; }
        public DbSet<Model.Trip> Trips { get; set; }
        public DbSet<Model.Person> Persons { get; set; }


        //Fluent API方式配置数据库：该方式必须重写DbContext类中的OnModelCreating()方法
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // base.OnModelCreating(modelBuilder);
            //modelBuilder.Entity<Model.Destination>().Property(d => d.Name).IsRequired();
            //首先获取Destination类的属性方法Property(lamdba表达式)点出方法IsRequired() 必须 
            //new DestinationMap();
            modelBuilder.Configurations.Add(new DestinationMap()); //.Add(new LodgingMap()); 可以使用链式编程
            //这样写可以避免 多次重复配置数据库 表中 的属性
            modelBuilder.Configurations.Add(new LodgingMap());

            modelBuilder.Configurations.Add(new TripMap());

            modelBuilder.Configurations.Add(new PersonMap());

            //Convention（协定、协议）,处理 Pluralizing(变形) PluralizingTableNameConvention处理数据表的单复数问题
            //modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); //去除复数重复
            // modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); //不明白
        }

    }

    /// <summary>
    /// 处理当前类的属性在数据库中配置问题
    /// </summary>
    public class DestinationMap : EntityTypeConfiguration<Destination>
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public DestinationMap()
        {
            Property(d => d.Name).IsRequired();
            Property(d => d.DestinationId).IsRequired();
            Property(d => d.Description).IsVariableLength();
        }

    }

    /// <summary>
    /// 处理当前类的属性在数据库中配置问题
    /// </summary>
    public class LodgingMap : EntityTypeConfiguration<Lodging>
    {
        /// <summary>
        /// 构造函数 
        /// </summary>
        public LodgingMap()
        {
            //Property(d => d.Destination).IsRequired();
            Property(l => l.Name).IsRequired();
            Property(l => l.LodgingId).IsRequired();
            Property(l => l.IsResort).IsRequired();
            Property(l => l.Owner).IsRequired();
        }
    }

    /// <summary>
    /// 处理数据库中属性配置问题
    /// </summary>
    public class TripMap : EntityTypeConfiguration<Trip>
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public TripMap()
        {
            //为当前对象设置 主键  
            //因为该字段的类型是Guid类型，就算在这里设置了主键 在数据库中也不会是自动增长的

           this.HasKey(t => t.Identifier);

            //为Guid类型 数据主键 设置自动增长
         

            this.Property(t => t.Identifier).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            Property(t => t.CostUSD).IsRequired();
            Property(t => t.EndDate).IsRequired();
            Property(t => t.StartDate).IsRequired();
              
        }

    }

    /// <summary>
    /// 处理数据库中属性配置问题
    /// </summary>
    public class PersonMap : EntityTypeConfiguration<Person>
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public PersonMap()
        {
            //设置主键
            this.HasKey(p => p.SocialSecurityNumber);
            Property(p => p.LastName).IsRequired();
            Property(p => p.FirstName).IsRequired();
        }

    }


}
